package com.simon.annual.web;

import java.io.File;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.simon.annual.entity.ReportNeeq;
import com.simon.annual.service.IAbortManager;
import com.simon.annual.service.IAccountingAndFinance;
import com.simon.annual.service.ICompanyPublicInfo;
import com.simon.annual.service.IDeclarationPrompt;
import com.simon.annual.service.IEquityShareholderInfo;
import com.simon.annual.service.IExplanation;
import com.simon.annual.service.IGovernance;
import com.simon.annual.service.IMajorIssues;
import com.simon.annual.service.IManagement;
import com.simon.annual.service.IStockissue;

@Controller
@RequestMapping("/index")
public class TestControl {
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	//第二节
	@Resource
	private ICompanyPublicInfo companyPublicInfo;
	//第四节
	@Resource
	private IAbortManager abortManager;
	//第三节
	@Resource
	private IAccountingAndFinance accountingAndFinance;
	//第一节
	@Resource
	private IDeclarationPrompt declarationPrompt;
	//释意
	@Resource
	private IExplanation explanation;
	//第五节
	@Resource
	private IMajorIssues majorIssues;
	//第六节
	@Resource
	private IEquityShareholderInfo equityShareholderInfo;
	//第七节
	@Resource
	private IStockissue stockissue;
	//第八节
	@Resource
	private IManagement management;
	//第九节
	@Resource
	private IGovernance governance;
	
	@Resource
	private Environment env;
	
	@RequestMapping("/test")
	public void test() {
		File[] file = new File("D:\\my_worksapce\\2016xml").listFiles();
		int tot = file.length;
		int error =0;
		for (File f : file) {
			boolean p =false;
			ReportNeeq reportNeeq = new ReportNeeq();
			Document doc = companyPublicInfo.init(f.getPath());
			//查询股票代码表
//			int companyPtrId =0;
//			try{
//				companyPtrId = companyPublicInfo.getCompanyPtrId(doc);
//			}catch(Exception e){
//				log.info(f.getPath()+"------------------查询股票代码错误--------------------");
//				p=true;
//				error++;
//				continue;
//			}
			int companyPtrId = Integer.parseInt(f.getName().split("\\.")[0]);
			
			try{
				explanation.analysis(doc, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------释意处理错误--------------------");
				p =true;
			}
			try{
				declarationPrompt.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第一节处理错误--------------------");
				p = true;
			}
			try{
				companyPublicInfo.analysis(doc, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第二节处理错误--------------------");
				p =true;
			}
			try{
				accountingAndFinance.analysis(doc, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第三节处理错误--------------------");
				p =true;
			}
			try{
				reportNeeq = abortManager.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第四节处理错误--------------------");
				p = true;
			}
			try{
				reportNeeq = majorIssues.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第五节处理错误--------------------");
				p = true;
			}
			try{
				reportNeeq = equityShareholderInfo.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第六节处理错误--------------------");
				p = true;
			}
			try{
				reportNeeq = stockissue.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第七节处理错误--------------------");
				p = true;
			}
			try{
				reportNeeq = management.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第八节处理错误--------------------");
				p = true;
			}
			try{
				governance.analysis(doc, reportNeeq, companyPtrId);
			}catch(Exception e){
				log.info(f.getPath()+"------------------第九节处理错误--------------------");
				p = true;
			}
			if(p){
				error++;
			}
		}
		log.info("失败数："+error+" 总数："+tot +" 成功数："+(tot-error));
	}
	
	@RequestMapping("/test1")
	public void test1() {
		File[] file = new File("D:\\my_worksapce\\2016年报xml").listFiles();
		int tot = file.length;
		int error =0;
		for (File f : file) {
			boolean p =false;
			try{
				System.out.println(f.getPath());
				Document doc = companyPublicInfo.init(f.getPath());
				int companyPtrId = Integer.parseInt(f.getName().split("\\.")[0]);
				ReportNeeq reportNeeq = new ReportNeeq();
//				reportNeeq = majorIssues.analysis(doc, reportNeeq, companyPtrId);
				governance.analysis(doc, reportNeeq,companyPtrId);
				System.out.println("end...");
			}catch(Exception e){
				e.printStackTrace();
				log.info("处理失败文件名："+f.getPath());
				p = true;
			}
			if(p){
				error++;
			}
		}
		log.info("失败数："+error+" 总数："+tot +" 成功数："+(tot-error));
	}
	
	@RequestMapping("/test2")
	public void test2() {
		Document doc = companyPublicInfo.init("D:\\my_worksapce\\2016年报xml\\836634.xml");
		int companyPtrId =0;
		ReportNeeq reportNeeq = new ReportNeeq();
		reportNeeq = majorIssues.analysis(doc, reportNeeq, companyPtrId);
//		accountingAndFinance.analysis(doc, companyPtrId);
		System.out.println(companyPtrId);
	}
}
